package com.lcf.system.controller;

import cn.dev33.satoken.annotation.SaCheckSafe;
import com.lcf.framework.annotation.PermissionCheck;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dto.user.TenantAddUserDTO;
import com.lcf.system.query.user.SysTenantUserPageQuery;
import com.lcf.system.service.SysTenantUserService;
import com.lcf.system.vo.tenant.SysTenantVO;
import com.lcf.system.vo.tenantUser.SysTenantUserPageVO;
import com.lcf.system.vo.tenantUser.SysTenantUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.lcf.framework.enums.PermissionEnum.*;

/**
 * 租户-用户(SysTenantUser)表控制层
 *
 * @author lcf
 * @since 2025-03-02 23:00:38
 */
@Tag(name = "租户-员工管理")
@RestController
@RequestMapping("sysTenantUser")
public class SysTenantUserController {
    @Resource
    private SysTenantUserService sysTenantUserService;

    /**
     * 租户添加用户
     */
    @Operation(summary = "租户添加用户")
    @PermissionCheck(SYS_TENANT_USER_ADD)
    @PostMapping("add")
    public void add(@Validated TenantAddUserDTO dto) {
        sysTenantUserService.add(dto);
    }

    /**
     * 退出当前租户
     */
    @Operation(summary = "用户主动退出当前租户")
    @SaCheckSafe
    @DeleteMapping("quit")
    public void quit() {
        sysTenantUserService.quit();
    }

    /**
     * 踢出当前租户
     */
    @Operation(summary = "将用户踢出当前租户")
    @SaCheckSafe
    @PermissionCheck(SYS_TENANT_USER_KICK_OUT)
    @DeleteMapping("kickOut/{userId}")
    public void kickOut(@PathVariable @Schema(description = "用户ID") long userId) {
        sysTenantUserService.kickOut(userId);
    }

    /**
     * 租户分页查询用户
     */
    @Operation(summary = "租户分页查询用户")
    @PermissionCheck(SYS_TENANT_USER_PAGE)
    @GetMapping("page")
    public PageVO<SysTenantUserPageVO> page(SysTenantUserPageQuery query) {
        return sysTenantUserService.page(query);
    }

    /**
     * 查看我的信息
     */
    @Operation(summary = "查看租户下我的详情")
    @GetMapping
    public SysTenantUserVO getDetail() {
        return sysTenantUserService.getDetail(UserUtils.getUserId());
    }

    /**
     * 查看用户信息
     */
    @Operation(summary = "查看租户下用户详情")
    @PermissionCheck(SYS_TENANT_USER_GET_DETAIL)
    @GetMapping("{userId}")
    public SysTenantUserVO getDetail(@PathVariable @Schema(description = "用户id") long userId) {
        return sysTenantUserService.getDetail(userId);
    }

    /**
     * 查看用户加入的租户列表
     */
    @Operation(summary = "查看用户加入的租户列表")
    @PermissionCheck(SYS_TENANT_USER_TENANT_LIST)
    @GetMapping("tenant/page/{userId}")
    public List<SysTenantVO> userTenantList(@PathVariable @Schema(description = "用户id") long userId) {
        return sysTenantUserService.userTenantList(userId);
    }

    /**
     * 查看我加入的租户列表
     */
    @Operation(summary = "查看我加入的租户列表")
    @GetMapping("tenant/page/mine")
    public List<SysTenantVO> myTenantList() {
        return sysTenantUserService.userTenantList(UserUtils.getUserId());
    }
}
